package app.service.goods;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import app.model.goods.GoodsStockRecords;
import app.util.StringUtil;
import base.PageInfo;
import base.category.sys.YesOrNo;
import base.dao.HibernateDao;
import base.service.BaseService;

@Service
public class GoodsStockRecordsService extends BaseService {

	@Autowired
	private HibernateDao hibernateDao;
	
	@Override
	public HibernateDao getDao() {
		return this.hibernateDao;
	}
	
	@Resource
	private GoodsStockService goodsStockService;
	
	/**
	 * 查询商品
	 * @param pageInfo
	 * @param goodsName 
	 * @return
	 */
	public PageInfo list(PageInfo pageInfo, Map<String, Object> parameterMap) {
		
		String goodsName = (String)parameterMap.get("goodsName");
		String beginTime = (String)parameterMap.get("beginTime");
		String endTime = (String)parameterMap.get("endTime");
		
		
		String sql = "select a.*,b.goods_unit,a.goods_num*a.goods_price goods_total_price from goods_stock_records a,goods_base b where a.goods_id = b.id and b.is_use = "+YesOrNo.YES_INT;
		if(StringUtil.isNotEmpty(goodsName)){
			sql += " and b.goods_name like '%"+goodsName+"%'";
		}
		if (StringUtil.isNotEmpty(beginTime)) {
			beginTime = beginTime.substring(0,beginTime.indexOf("T"));
			sql += " and date_format(a.create_time,'%Y-%m-%d') >= '" + beginTime + "'";
        }
        if (StringUtil.isNotEmpty(endTime)) {
        	endTime = endTime.substring(0,endTime.indexOf("T"));
        	sql += " and date_format(a.create_time,'%Y-%m-%d') <= '" + endTime + "'";
        }
		sql += " order by a.create_time desc";
		PageInfo page = getDao().queryPageListBySql(pageInfo, sql);
		return page;
	}

	public void saveEntity(GoodsStockRecords entity) {
		getDao().save(entity);
	}

	public void updateEntity(GoodsStockRecords entity) {
		getDao().mergeAndUpdate(entity);
	}

	public GoodsStockRecords queryEntityById(String id) {
		String sql = "select * from goods_stock_records a where a.id = '"+id+"'";
		Object entity = getDao().queryEntityBySql(sql , GoodsStockRecords.class);
		return (GoodsStockRecords)entity;
	}

	public void delEntity(String ids) {
		if(!StringUtils.isEmpty(ids)){
			String[] idsArray = ids.split(",");
			for (int i = 0; i < idsArray.length; i++) {
				GoodsStockRecords gb = new GoodsStockRecords();
				gb.setId(idsArray[i]);
				getDao().del(gb);
			}
		}
	}
}
